package com.beardedcoder.lowPower.activities;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.TextView;
import com.beardedcoder.lowPower.R;
import com.beardedcoder.lowPower.app.AppSettings;
import com.beardedcoder.lowPower.service.MyServiceConnector;
import com.beardedcoder.lowPower.service.PowerService;
import com.beardedcoder.utils.UtilBatteryInterpreter;
import com.beardedcoder.utils.UtilScreen;
import com.beardedcoder.utils.power.AirplaneFeatures;
import com.beardedcoder.utils.ui.ActionBar;
import com.beardedcoder.utils.ui.ActionBar.*;

public class Launch extends Activity {
	
	private ActionBar mActionbar;
	private SeekBar mBrightnessValue;
	private TextView mUserBrightnessReport;
	private CheckBox mBrightnessAutoAdjust;
	
	private SeekBar mBatterySeekBar;
	private TextView mBatteryPercent;
	private TextView mBatteryData;
	
	private MyServiceConnector mServiceConnection;
	private BroadcastReceiver mBroadcastReceiverBatteryChange;
	private AirplaneFeatures airplaneFeatures;
	
	protected void onPause() {
		super.onPause();
		if (mServiceConnection != null) {
			mServiceConnection.doUnbindService();			
		}
		unRegisterReceivers();		
	}

	public void onStart(){
		super.onStart();
		Intent service = new Intent(getApplicationContext(), PowerService.class);
		startService(service);
		registerReceivers();
	}
	
	@Override   
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		airplaneFeatures = new AirplaneFeatures(this);
		
		configureViews();
		configureLayouts();
		AppSettings.log(this.getClass(), "onCreate", getResources().getDisplayMetrics().toString());
	}

    private void configureLayouts(){
    	double percentage = UtilScreen.getBrightnessSystem(this);
    	int progress = (int) (UtilScreen.BRIGHTNESS_MAX_WINDOW * percentage);
    	mBrightnessValue.setProgress(progress);
    	mBrightnessAutoAdjust.setChecked(UtilScreen.isBrightnessValueAutoAdjust(Launch.this));
	}
    
    private void configureViews(){
    	configureActionbar();
    	
    	mBatteryData = (TextView) findViewById(R.id.launch_screen_battery_extra_message);
    	mBatteryPercent = (TextView) findViewById(R.id.launch_screen_battery_tv_percentage);
    	mBatterySeekBar = (SeekBar) findViewById(R.id.launch_screen_battery_seekbar_value);
    	
    	mBrightnessAutoAdjust = (CheckBox) findViewById(R.id.launch_screen_auto_adjust_brightness);
    	OnCheckedChangeListener listener = new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				UtilScreen.setBrightnessValueAutoAdjust(Launch.this, isChecked);
				UtilScreen.setBrightnessWindowForceRefresh(Launch.this);
				mBrightnessValue.setEnabled(!isChecked);
			}
		};		
		mBrightnessAutoAdjust.setOnCheckedChangeListener(listener);
    	mUserBrightnessReport = (TextView) findViewById(R.id.launch_screen_brightness_message);
    	mBrightnessValue = (SeekBar) findViewById(R.id.layout_seekbar_brightness_value);
    	mBrightnessValue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    		@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
    			double percentage = (double) seekBar.getProgress() / 100f;
    			UtilScreen.setBrightnessSystem(Launch.this, percentage);
    		}
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) { }
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    			mUserBrightnessReport.setText(progress + "%");
    			double percentage = (double) seekBar.getProgress() / 100f;
    			UtilScreen.setBrightnessWindow(Launch.this, percentage);
			}
		});
    }

	private void configureActionbar() {
		mActionbar = (ActionBar) findViewById(R.id.actionbar);
		
		ActionImage settings = new ActionBar.ActionImage(this) {
			public void actionClicked() {
				Intent launchSettingsActivity = new Intent(Launch.this, SettingsActivity.class);
				startActivity(launchSettingsActivity);
			}
		};
		settings.setBackgroundResource(R.drawable.ic_action_setting);
		mActionbar.addAction(settings, ActionBar.CONTAINER_RIGHT);
		
		ActionImage close = new ActionBar.ActionImage(this) {
			public void actionClicked() {
				finish();
				// TODO: make new settings screen.
			}
		};
		close.setBackgroundResource(R.drawable.ic_action_close);
		mActionbar.addAction(close, ActionBar.CONTAINER_RIGHT);
		
		
    	ActionText title = new ActionBar.ActionText(this) {
			public void actionClicked() { }
		};
		title.setText("Main Screen");
		title.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
		title.setTextColour(getResources().getColor(R.color.low_power_black));
		mActionbar.addAction(title, ActionBar.CONTAINER_TITLE);
	}
		
    private void registerReceivers() {
    	mBroadcastReceiverBatteryChange = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent){
				int flagsToUse = 
					UtilBatteryInterpreter.FLAG_REPORT_LEVEL + 
					UtilBatteryInterpreter.FLAG_REPORT_TEMPERATURE +
					UtilBatteryInterpreter.FLAG_REPORT_VOLTAGE +
					UtilBatteryInterpreter.FLAG_REPORT_HEALTH +
					UtilBatteryInterpreter.FLAG_REPORT_CURRENT_STATUS;
				mBatteryData.setText(
						UtilBatteryInterpreter.getBatteryData(intent, flagsToUse));
				mBatteryPercent.setText(intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0) +"%");
				mBatterySeekBar.setEnabled(true);
				mBatterySeekBar.setProgress(intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0));
				mBatterySeekBar.setEnabled(false);
			}
		};
		IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
		registerReceiver(mBroadcastReceiverBatteryChange, filter);
	}
    
    private void unRegisterReceivers(){
    	if (mBroadcastReceiverBatteryChange != null) {
    		unregisterReceiver(mBroadcastReceiverBatteryChange);
    		mBroadcastReceiverBatteryChange = null;
    	}
    }
    
    public void onClickWifi (View view) {
    	Intent intent = new Intent(this, ActivityWifi.class);
    	startActivity(intent);
    }
    
    public void onClickCell (View view) {
    	Intent intent = new Intent(this, ActivityCell.class);
    	startActivity(intent);    	
    }
    
    public void onClickAirplaneOn (View view) {
    	airplaneFeatures.activate();
    }
    
    public void onClickAirplaneOff (View view) {
    	airplaneFeatures.deactivate();
    }
    
    public void onClickHomeScreen (View view) {
    	startActivity(new Intent(this, ActivityHomeScreen.class));
    }

}